/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.alien.admin.modules.publisher.domain;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.sql.Timestamp;
import java.util.Set;

/**
* @website https://el-admin.vip
* @description /
* @author zengzhifang
* @date 2021-10-18
**/
@Entity
@Data
@Table(name="dp_publish_sheet_flow")
public class PublishSheetFlow implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    @ApiModelProperty(value = "id")
    private Long id;

    @Column(name = "title")
    @ApiModelProperty(value = "标题，格式如（服务[ztjy-file-serve#ztjy-upload]发布申请-某某人）")
    private String title;

    @Column(name = "flow_no",nullable = false)
    @NotBlank
    @ApiModelProperty(value = "流程编号")
    private String flowNo;

    @Column(name = "publish_time",nullable = false)
    @NotNull
    @ApiModelProperty(value = "发布时间")
    private Timestamp publishTime;

    @Column(name = "applier_id",nullable = false)
    @NotNull
    @ApiModelProperty(value = "申请人id")
    private Long applierId;

    @Column(name = "publish_type",nullable = false)
    @NotNull
    @ApiModelProperty(value = "发布类型 0-需求发布 1-内部优化 2-修复bug 3-特殊需求")
    private Integer publishType;

    @Column(name = "db_changed",nullable = false)
    @NotNull
    @ApiModelProperty(value = "涉及线上库变动（0-否 1-是）")
    private Integer dbChanged;

    @Column(name = "cache_changed",nullable = false)
    @NotNull
    @ApiModelProperty(value = "涉及缓存逻辑变动（0-否 1-是）")
    private Integer cacheChanged;

    @Column(name = "risk_assessment")
    @ApiModelProperty(value = "风险评估")
    private String riskAssessment;

    @Column(name = "db_script")
    @ApiModelProperty(value = "数据库脚本（涉及库变动显示)")
    private String dbScript;

    @Column(name = "rollback_db_script")
    @ApiModelProperty(value = "回滚脚本（涉及库变动显示)")
    private String rollbackDbScript;

    @Column(name = "configserver_script")
    @ApiModelProperty(value = "统配")
    private String configserverScript;

    @Column(name = "cache_script")
    @ApiModelProperty(value = "缓存脚本")
    private String cacheScript;

    @Column(name = "remarks")
    @ApiModelProperty(value = "备注")
    private String remarks;

    @Column(name = "has_tester",nullable = false)
    @NotNull
    @ApiModelProperty(value = "测试介入（0-否 1-是）")
    private Integer hasTester;

    @Column(name = "tester_id")
    @ApiModelProperty(value = "测试人员（测试介入是必选）")
    private Long testerId;

    @Column(name = "deployer_id",nullable = false)
    @NotNull
    @ApiModelProperty(value = "运维人员")
    private Long deployerId;

    @Column(name = "manager_id")
    @ApiModelProperty(value = "开发经理（角色为开发经理的用户列表）")
    private Long managerId;

    @Column(name = "create_time")
    @ApiModelProperty(value = "创建时间")
    private Timestamp createTime;

    @Column(name = "end_time")
    @ApiModelProperty(value = "结束时间")
    private Timestamp endTime;

    @Column(name = "deploy_num")
    @ApiModelProperty(value = "部署次数")
    private Integer deployNum;

    @Column(name = "flow_node_id",nullable = false)
    @ApiModelProperty(value = "0-起草 1-申请单提交（待主管审核）2-待测试确认 3.待运维RC确认4.待测试人员RC发布确认5.待开发人员RC发布确认  6.待运维PROD确认7.待测试PROD发布确认8.待开发人员发布确认 9.结束")
    private Integer flowNodeId;

    @Column(name = "processer_id")
    @ApiModelProperty(value = "当前节点处理人id")
    private Long processerId;

    @Column(name = "flow_status",nullable = false)
    @ApiModelProperty(value = "0-草稿 1-进行中 2.结束 3.驳回")
    private Integer flowStatus;

    /**
     * 是否是草稿
     */
    @Transient
    private Boolean isDraft;

    @OneToMany(cascade={CascadeType.PERSIST,CascadeType.REMOVE}, fetch = FetchType.EAGER)
    @JoinColumn(name = "flow_id", updatable = false, insertable = false)
    private Set<PublishProject> publishProjects;

    @OneToMany(cascade={CascadeType.PERSIST,CascadeType.REMOVE}, fetch = FetchType.EAGER)
    @JoinColumn(name = "flow_id", updatable = false, insertable = false)
    private Set<PublishDeploy> publishDeploys;


    public void copy(PublishSheetFlow source){
        BeanUtil.copyProperties(source,this, CopyOptions.create().setIgnoreNullValue(true));
    }
}